package com.seadragon.app.ksr.dao.jpa;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.seadragon.app.ksr.dao.CountyLookUpDao;
import com.seadragon.app.ksr.model.tiger.CountyLookUp;

public class CountyLookUpDaoJpaImpl extends AbstractJpaDao implements CountyLookUpDao {

	@Override
	public CountyLookUp findByCountyName(String countyName, String stateName) {
		CriteriaBuilder builder = entityManager.getCriteriaBuilder();
		CriteriaQuery<CountyLookUp> cq = builder.createQuery(CountyLookUp.class);
		Root<CountyLookUp> root = cq.from(CountyLookUp.class);
		cq.select(root);
		cq.where(builder.and(builder.equal(root.get("name"), countyName), builder.equal(root.get("state"), stateName)));

		return entityManager.createQuery(cq).getSingleResult();
	}

	@Override
	public List<CountyLookUp> findSiblingByCountyName(String countyName, String stateName) {
		CriteriaBuilder builder = entityManager.getCriteriaBuilder();
		CriteriaQuery<CountyLookUp> cq = builder.createQuery(CountyLookUp.class);
		Root<CountyLookUp> root = cq.from(CountyLookUp.class);
		cq.select(root);
		cq.where(builder.and(builder.equal(root.get("name"), countyName), builder.equal(root.get("state"), stateName)));

		return entityManager.createQuery(cq).getResultList();
	}

}
